const express = require('express')
const request = require('request')
const router = express.Router()

// 获取疫情信息
router.get('/', (req, res) => {
  const { area = 'china' } = req.query
  let name
  switch(area) {
    case 'china':
      name = 'disease_h5'
      break
    case 'foreign':
      name = 'disease_foreign'
      break
    default:
      res.json({
        code: 200,
        data: {
          message: '地区有误'
        }
      })
      return
  }
  // 从腾讯接口获取数据
  request('https://view.inews.qq.com/g2/getOnsInfo?name=' + name, (err, response, body) => {
    if (err) {
      res.json({
        code: 200,
        data: {
          err
        }
      })
      return
    }

    res.json({
      code: 200,
      data: JSON.parse(JSON.parse(body).data)
    })
  })
})

module.exports = router
